package com.asen.commons.core.base.enums;

import com.asen.commons.core.base.vo.FieldVo;
import com.asen.commons.core.base.StringEnum;
import io.swagger.annotations.ApiModel;

import java.util.Date;

/**
 * 字段查询类型
 *
 * @author menggangaaa
 * @date 2022/10/26
 **/
@ApiModel("字段查询类型")
public enum FieldQueryTypeEnum implements StringEnum {
    BOOL("bool", "布尔型"),
    INT("int", "整型"),
    NUMBER("number", "数值型"),
    STRING("string", "字符型"),
    DATE("date", "日期型"),
    ENUM("enum", "枚举型"),
    OTHER("other", "其他类型"),
    ;

    private String value;

    private String desc;

    FieldQueryTypeEnum(String value, String desc) {
        this.value = value;
        this.desc = desc;
    }

    @Override
    public String getValue() {
        return value;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    /**
     * 构建查询类型
     */
    public static void setQueryType(FieldVo fieldVo) {
        String javaType = fieldVo.getJavaType();
        String type = fieldVo.getType();
        FieldQueryTypeEnum typeEnum = OTHER;
        if ("Enum".equals(type)) {
            typeEnum = ENUM;
        } else {
            Class<?> clazz = null;
            try {
                clazz = Class.forName(javaType);
            } catch (ClassNotFoundException ignored) {
            }
            if (Boolean.class.getName().equals(javaType)) {
                typeEnum = BOOL;
            } else if (Integer.class.getName().equals(javaType)) {
                typeEnum = INT;
            } else if (clazz != null && Number.class.isAssignableFrom(clazz)) {
                typeEnum = NUMBER;
            } else if (Date.class.getName().equals(javaType)) {
                typeEnum = DATE;
            } else if (String.class.getName().equals(javaType)) {
                typeEnum = STRING;
            }
        }
        fieldVo.setQueryType(typeEnum.value);
    }
}
